/**
 *  编辑页
 *
 * 作者 admin
 * 日期 2024-08-07 16:43:01
 */
layui.use(['jquery', 'febs', 'form', 'validate', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        laydate = layui.laydate,
        form = layui.form,
        table = layui.table,
        fileUpload = layui.fileUpload,
        $view = $('#reportDetail-edit'),
        $accidentCategory = $('div[name="accidentCategory"]'),
        $accidentType = $('div[name="accidentType"]'),
        $accidentLevel = $('div[name="accidentLevel"]'),
        $emergencyLevel = $('div[name="emergencyLevel"]'),
        $accidentNature = $('div[name="accidentNature"]'),
        $violationSurvivalRules = $('div[name="violationSurvivalRules"]'),
        $accidentShipType = $('div[name="accidentShipType"]'),
        $accidentReason = $('div[name="accidentReason"]'),
        $directReason = $('div[name="directReason"]'),
        $indirectionReason = $('div[name="indirectionReason"]'),
        $rootReason = $('div[name="rootReason"]'),
        injuryUserTableIns,
        culpritUserTableIns,
        timelineTableIns,
        validate = layui.validate;
    let injuryUserTableData = [];
    let culpritUserTableData = [];
    let timelineTableData = [];
    let accidentReportFileList = [];
    let accidentSurveyFileList = [];
    let reportImgFileList = [];
    let reportAnalysisFileList = [];
    let teamList = [];

    initDictItemLabel_reportDetail();
    initFormValue();
    form.verify(validate);
    form.render();
    initInjuryUserTable();
    initCulpritUserTable();
    initTimelineTable();

    laydate.render({
        elem: "#accidentDate",
        done: function (value, date) {
        },
        type: 'datetime'
    })

    laydate.render({
        elem: "#surveyDeadLine",
        done: function (value, date) {
        },
        type: 'date'
    })

    laydate.render({
        elem: "#noticeDeptDate",
        done: function (value, date) {
        },
    })
    function initDictItemLabel_reportDetail() {
        febs.initDictItemLabel($accidentCategory, 'SAFETY_ACCIDENT_CATEGORY', accidentReportData.accidentCategory);
        febs.initDictItemLabel($accidentType, 'SAFETY_ACCIDENT_TYPE', accidentReportData.accidentType);
        febs.initDictItemLabel($accidentLevel, 'SAFETY_ACCIDENT_LEVEL', accidentReportData.accidentLevel);
        febs.initDictItemLabel($accidentShipType, 'SAFETY_SHIP_TYPE', accidentReportData.accidentShipType);
        febs.initDictItemLabel($emergencyLevel, 'SAFETY_ACCIDENT_EMERGENCY_LEVEL', accidentReportData.emergencyLevel);
        febs.initDictItemLabel($accidentNature, 'SAFETY_ACCIDENT_NATURE', accidentReportData.accidentNature);
        febs.initDictItemLabel($violationSurvivalRules, 'SAFETY_ACCIDENT_VIOLATION_SURVIVAL_RULES', accidentReportData.violationSurvivalRules);
        febs.initDictItemLabel($accidentReason, 'SAFETY_ACCIDENT_REASON', accidentReportData.accidentReason);
        febs.initDictItemLabel($directReason, 'SAFETY_ACCIDENT_DIRECT_REASON', accidentReportData.directReason);
        febs.initDictItemLabel($indirectionReason, 'SAFETY_ACCIDENT_INDIRECTION_REASON', accidentReportData.indirectionReason);
        febs.initDictItemLabel($rootReason, 'SAFETY_ACCIDENT_ROOT_REASON', accidentReportData.rootReason);
    }

    // 调查人员表格
    function initTeamListTable(data) {
        teamListTableIns = febs.table.init({
            elem: $view.find('table[id="teamListTable"]'),
            id: 'teamListTable',
            page: false,
            limit: Number.MAX_VALUE,
            data: data,
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'dutyName', title: '职能名称', width: 180},
                {
                    field: 'surveyUserList',
                    title: '小组成员',
                    templet: function (d) {
                        var flg = '';
                        var users = d.surveyUserList;
                        if(users){
                            users.forEach((item, index)=> {
                                flg += '<span class="layui-badge febs-bg-orange" style="margin-left:5px; ">'+item.surveyRealName+'</span>'
                            })
                        }
                        return flg;
                    }
                },
            ]],
            defaultToolbar: []
        });
    }

    function initFormValue() {
        if (accidentReportData) {
            //伤亡人员
            injuryUserTableData = accidentReportData.injuryUserDataList;
            //肇事人员
            culpritUserTableData = accidentReportData.culpritUserDataList;
            //时间线
            timelineTableData = accidentReportData.timelineList;
            //事故上报附件
            accidentReportFileList = accidentReportData.accidentReportFileList;
            if (accidentReportFileList && accidentReportFileList.length > 0) {
                fileUpload.render({
                    elem: '#uploadReportBtn',
                    // url:ctx + 'accident/upload',
                    type: '2',
                    maxNum: 5,
                    isDetail: true,
                    defaultData: accidentReportFileList,
                    done: function (res) {
                    }
                    , error: function () {
                        layer.msg("服务器异常")
                    }
                })
            }
            // 调查人员
            teamList = accidentReportData.teamList;
            if (teamList && teamList.length > 0) {
                initTeamListTable(teamList)
            }
            //事故调查附件
            accidentSurveyFileList = accidentReportData.accidentSurveyFileList;
            if ($("#uploadReportSurveyBtn") && accidentSurveyFileList && accidentSurveyFileList.length > 0) {
                fileUpload.render({
                    elem: '#uploadReportSurveyBtn',
                    url: '',
                    type: '2',
                    maxNum: 5,
                    isDetail: true,
                    defaultData: accidentSurveyFileList,
                    done: function (res) {
                    }
                    , error: function () {
                        layer.msg("服务器异常")
                    }
                })
            }

            //事故图解
            reportImgFileList = accidentReportData.reportImgFileList;
            if (reportImgFileList && reportImgFileList.length > 0) {
                fileUpload.render({
                    elem: '#fileUploadReportImg',
                    url: '',
                    type: '2',
                    maxNum: 5,
                    isDetail: true,
                    defaultData: reportImgFileList,
                    done: function (res) {
                    }
                    , error: function () {
                        layer.msg("服务器异常")
                    }
                })
            }

            //事故分析图
            reportAnalysisFileList = accidentReportData.reportAnalysisFileList;
            if (reportAnalysisFileList && reportAnalysisFileList.length > 0) {
                fileUpload.render({
                    elem: '#fileUploadReportAnalysis',
                    url: '',
                    type: '2',
                    maxNum: 5,
                    isDetail: true,
                    defaultData: reportAnalysisFileList,
                    done: function (res) {
                    }
                    , error: function () {
                        layer.msg("服务器异常")
                    }
                })
            }

            form.val("reportSurveyAudit-edit-form", {
                "reportId": accidentReportData.reportId,
                "tenantId": accidentReportData.tenantId,
                "accidentNo": accidentReportData.accidentNo,
                "accidentTitle": accidentReportData.accidentTitle,
                "accidentDate": accidentReportData.accidentDate,
                "accidentPlace": accidentReportData.accidentPlace,
                "accidentType": accidentReportData.accidentType,
                "accidentLevel": accidentReportData.accidentLevel,
                "accidentShipType": accidentReportData.accidentShipType,
                "preliminarilyDamage": accidentReportData.preliminarilyDamage,
                "preliminarilyDescript": accidentReportData.preliminarilyDescript,
                "acceptAssistFlg": accidentReportData.acceptAssistFlg,
                "surveyStatus": accidentReportData.surveyStatus,
                "surveyDeadline": accidentReportData.surveyDeadline,
                "surveyRemark": accidentReportData.surveyRemark,
                "projectId": accidentReportData.projectId,
                "projectName": accidentReportData.projectName,
                "accidentAreaId": accidentReportData.accidentAreaId,
                "accidentAreaName": accidentReportData.accidentAreaName,
                "noticeDeptFlg": accidentReportData.noticeDeptFlg,
                "noticeDeptName": accidentReportData.noticeDeptName,
                "noticeDeptDate": accidentReportData.noticeDeptDate,
                "accidentOverview": accidentReportData.accidentOverview,
                "accidentCategory": accidentReportData.accidentCategory,
                "detailPlace": accidentReportData.detailPlace,
                "accidentNature": accidentReportData.accidentNature,
                "violationSurvivalRules": accidentReportData.violationSurvivalRules,
                "accidentElapse": accidentReportData.accidentElapse,
                "emergencyOverview": accidentReportData.emergencyOverview,
                "emergencyLevel": accidentReportData.emergencyLevel,
                "totalLossHours": accidentReportData.totalLossHours,
                "environmentEffect": accidentReportData.environmentEffect,
                "totalLoss": accidentReportData.totalLoss,
                "economyLoss": accidentReportData.economyLoss,
                "indirectLoss": accidentReportData.indirectLoss,
                "accidentReason": accidentReportData.accidentReason,
                "directReason": accidentReportData.directReason,
                "directReasonRemark": accidentReportData.directReasonRemark,
                "indirectionReason": accidentReportData.indirectionReason,
                "indirectionReasonRemark": accidentReportData.indirectionReasonRemark,
                "rootReason": accidentReportData.rootReason,
                "rootReasonRemark": accidentReportData.rootReasonRemark,
                "accidentAnalysis": accidentReportData.accidentAnalysis,
                "obligationUserId": accidentReportData.obligationUserId,
                "obligationUserNo": accidentReportData.obligationUserNo,
                "obligationRealName": accidentReportData.obligationRealName,
                "obligationDeptId": accidentReportData.obligationDeptId,
                "obligationDeptName": accidentReportData.obligationDeptName,
                "obligationPostId": accidentReportData.obligationPostId,
                "obligationPostName": accidentReportData.obligationPostName,
                "obligationRatio": accidentReportData.obligationRatio,
                "obligationCognizance": accidentReportData.obligationCognizance,
                "obligationDisposeCondition": accidentReportData.obligationDisposeCondition,
                "status": accidentReportData.status,
                "deathToll": accidentReportData.deathToll,
                "severeInjuryToll": accidentReportData.severeInjuryToll,
                "slightInjuryToll": accidentReportData.slightInjuryToll,
                "missingToll": accidentReportData.missingToll,
                "noticeChic": accidentReportData.noticeChic,
                "noticeDate": accidentReportData.noticeDate,
                "surveyAuditUserId": accidentReportData.surveyAuditUserId,
                "surveyAuditRealName": accidentReportData.surveyAuditRealName,
                "surveyAuditDate": accidentReportData.surveyAuditDate,
                "surveyAuditRemark": accidentReportData.surveyAuditRemark,
                "cautionaryFlg": accidentReportData.cautionaryFlg,
                "workflowApplyId": accidentReportData.workflowApplyId,
                "reportAnalysisFileList": accidentReportData.reportAnalysisFileList,
                "reportImgFileList": accidentReportData.reportImgFileList,
                "surveyAuditRecordList": accidentReportData.surveyAuditRecordList,
                "rectificationOpinionList": accidentReportData.rectificationOpinionList,
            });
            //整改措施拼接html
            if (accidentReportData.rectificationOpinionList) {
                let reasonList = accidentReportData.rectificationOpinionList;
                for (let i = 0; i < reasonList.length; i++) {
                    appendReportReason(i, reasonList[i]);
                }
            }
            form.render('checkbox');
        }
    }

    function appendReportReason(i, item) {
        var groupPanelAppend = '<div class="layui-panel" id="panel_reason0" >' +
            '                                            <div class="layui-field-box" name="reasonBox">' +
            '                                                <span class="layui-badge febs-bg-cyan" >' + (i + 1) + '</span>' +
            '                                                <div class="layui-form-item">' +
            '                                                    <div class="layui-row">' +
            '                                                        <div name="ifShow' + i + '">' +
            '                                                            <div class="layui-col-xs4" >' +
            '                                                                <label class="layui-form-label layui-form-label-detail">整改责任人：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >' + item.obligationRealName+'('+item.obligationUserNo+'|'+item.obligationDeptName+')' + '' +
            '                                                                </div>' +
            '                                                            </div>' +
            '                                                            <div class="layui-col-xs4" >' +
            '                                                                <label class="layui-form-label layui-form-label-detail">整改期限：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >' + item.rectificationDeadline + '' +
            '                                                                </div>' +
            '                                                            </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                                <div class="layui-form-item " name="ifShow' + i + '">' +
            '                                                    <div class="layui-row">' +
            '                                                        <div class="layui-col-xs12">' +
            '                                                            <label class="layui-form-label layui-form-label-detail">整改措施：</label>' +
            '                                                                <div class="layui-input-block layui-input-block-html" >' + item.rectificationOpinion + '' +
            '                                                                </div>' +
            '                                                        </div>' +
            '                                                    </div>' +
            '                                                </div>' +
            '                                            </>' +
            '                                        </div>'
        ;

        $("#reason_box").append(groupPanelAppend);
        form.render();
    }

    function initCulpritUserTable() {
        culpritUserTableIns = febs.table.init({
            elem: $view.find('#culpritUserTable'),
            id: 'culpritUserTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            css: [ // 设置单元格样式
                // 取消默认的溢出隐藏，并设置适当高度
                '.layui-table-cell{height: 50px; line-height: 40px;}',
                '.layui-table-cell .layui-colorpicker{width: 38px; height: 38px;}',
                '.layui-table-cell select{height: 36px; padding: 0 5px;}'
            ].join(''),
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'userId', title: 'id',hide:true},
                {field: 'userName', title: 'userName',hide:true},
                {field: 'userNo', title: 'userNo',hide:true},
                {field: 'deptId', title: 'deptId',hide:true},
                {field: 'realName',align:'center', title: '姓名', width: 140},
                {field: 'workplace',align:'center', title: '工作单位', minWidth: 150,hide:true},
                {field: 'deptName',align:'center', title: '部门', minWidth: 150},
                {field: 'joinDate',align:'center', title: '入司时间', minWidth: 140},
                {field: 'userType',align:'center', title: '用工形式', width: 100, templet: function (d) {
                        var flg = '';
                        switch (d.userType) {
                            case "1": {
                                flg = '<span>本工</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>外包工</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span>外协工</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'userSeniority',align:'center', title: '工龄(年)', width: 120,hide:true},
                {field: 'currentSeniority',align:'center', title: '本工种工龄', width: 120},
                {field: 'userDegree',align:'center', title: '学历', width: 100},
                {field: 'identificationNo',align:'center', title: '身份证号', width: 170},
                {field: 'lossHours',align:'center', title: '损失工时', minWidth: 130},
            ]],
            done: function(res, curr, count){
            },
            data:culpritUserTableData
        });
    }


    function initInjuryUserTable() {
        injuryUserTableIns = febs.table.init({
            elem: $view.find('#injuryUserTable'),
            id: 'injuryUserTable',
            page: false,
            skin: '',
            limit: Number.MAX_VALUE,
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'userId', title: 'id',hide:true},
                {field: 'userName', title: 'userName',hide:true},
                {field: 'userNo', title: 'userNo',hide:true},
                {field: 'deptId', title: 'deptId',hide:true},
                {field: 'realName',align:'center', title: '姓名', width: 100},
                {field: 'workplace',align:'center', title: '工作单位', minWidth: 150,hide:true},
                {field: 'deptName',align:'center', title: '部门', minWidth: 150},
                {field: 'joinDate',align:'center', title: '入司时间', minWidth: 140},
                {field: 'userType',align:'center', title: '用工形式', width: 100, templet: function (d) {
                        var flg = '';
                        switch (d.userType) {
                            case "1": {
                                flg = '<span>本工</span>'
                                break;
                            }
                            case "2": {
                                flg = '<span>外包工</span>'
                                break;
                            }
                            case "3": {
                                flg = '<span>外协工</span>'
                                break;
                            }
                        }
                        return flg;
                    }
                },
                {field: 'userSeniority',align:'center', title: '工龄(年)', width: 100,hide:true},
                {field: 'currentSeniority',align:'center', title: '本工种工龄', width: 120},
                {field: 'userDegree',align:'center', title: '学历', width: 60,hide:true},
                {field: 'identificationNo',align:'center', title: '身份证号', width: 170},
                {field: 'injuryPlace',align:'center', title: '受伤部位', minWidth: 140},
                {field: 'injuryLevel',align:'center',title: '伤害程度',templet:'#injury-level', minWidth: 150},
                {field: 'lossHours',align:'center', title: '损失工时', minWidth: 130},
            ]],
            done: function (res, curr, count) {
            },
            data: injuryUserTableData
        });
    }

    function initTimelineTable() {
        timelineTableIns = febs.table.init({
            elem: $view.find('#timelineTable'),
            id: 'timelineTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            css: [ // 设置单元格样式
                // 取消默认的溢出隐藏，并设置适当高度
                '.layui-table-cell{height: 50px; line-height: 40px;}',
                '.layui-table-cell .layui-colorpicker{width: 38px; height: 38px;}',
                '.layui-table-cell select{height: 36px; padding: 0 5px;}'
            ].join(''),
            cols: [[
                {title: '序号', type: 'numbers',width: 60},
                {field: 'id',align:'center',title: 'id', hide:true},
                {field: 'accidentTime',align:'center',title: '时间',templet:'#accident-time', width: 180},
                {field: 'accidentContent', title: '描述', minWidth: 500},
            ]],
            done: function(res, curr, count){
            },
            data:timelineTableData
        });
    }

});